//
//  WLDatePicker.m
//  WanLian
//
//  Created by muzhuang on 2018/10/9.
//  Copyright © 2018年 wanlian. All rights reserved.
//

#import "WLDatePicker.h"

@implementation WLDatePicker

- (void)showWithFrame:(CGRect)frame andDatePickerMode:(UIDatePickerMode)mode
{
    self.frame = [[UIScreen mainScreen]bounds];
    self.backgroundColor = wlRGBACOLOR(0, 0, 0, 0.5);
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapPickerBack)];
    [self addGestureRecognizer:tapGesture];
    
    UIView *vback = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT_SCREEN_WL, frame.size.width, frame.size.height)];
    vback.backgroundColor = [UIColor whiteColor];
    if(!self.picker)
    {
        _picker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, WLScaleX(60), frame.size.width, frame.size.height - WLScaleX(60))];
        _picker.locale = [NSLocale currentLocale];
        _picker.date = [NSDate date];
        _picker.maximumDate = [NSDate date];
    }
    
    self.picker.datePickerMode = mode;
//    [self.picker addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
    [vback addSubview:self.picker];
    
    UIButton *btnCancel = [[UIButton alloc] initWithFrame:CGRectMake(8, WLScaleX(5), 50, WLScaleX(50))];
    btnCancel.titleLabel.font = [WLCommonTool fontTypePingFangSCRegular:17];
    [btnCancel setTitleColor:wlDarkGrayColor forState:UIControlStateNormal];
    [btnCancel setTitle:@"取消" forState:UIControlStateNormal];
    [btnCancel addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    [vback addSubview:btnCancel];
    
    UIButton *btnDone = [[UIButton alloc] initWithFrame:CGRectMake(WIDTH_SCREEN_WL-58, WLScaleX(5), 50, WLScaleX(50))];
    btnDone.titleLabel.font = [WLCommonTool fontTypePingFangSCRegular:17];
    [btnDone setTitleColor:wlUIColorRGB(0x1A59E2) forState:UIControlStateNormal];
    [btnDone setTitle:@"完成" forState:UIControlStateNormal];
    [btnDone addTarget:self action:@selector(doneAction) forControlEvents:UIControlEventTouchUpInside];
    [vback addSubview:btnDone];
    
    [self addSubview:vback];
    [UIView animateWithDuration:0.25 animations:^{
        vback.frame = frame;
    }];
//    [[UIApplication sharedApplication].keyWindow addSubview:self];
}

- (void)tapPickerBack
{
    [self dismiss];
}

- (void)dismiss
{
    [self removeFromSuperview];
    if (tapGesture) {
        [self removeGestureRecognizer:tapGesture];
    }
}

- (void)doneAction
{
    if([self.delegate respondsToSelector:@selector(picker:valueChanged:)])
    {
        [self.delegate picker:self.picker valueChanged:_picker.date];
    }
    [self dismiss];
}

- (void)valueChanged:(UIDatePicker *)picker
{
    if([self.delegate respondsToSelector:@selector(picker:valueChanged:)]){
        [self.delegate picker:picker valueChanged:picker.date];
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
